﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_pages : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["username"].ToString().Length == 0)
                Response.Redirect("login.aspx");

            clsMenu clsMN = new clsMenu();
            clsMN.exportDataToDropDownList(ref cmbMenu);
        }
    }
    protected void butOK_Click(object sender, EventArgs e)
    {
        //Upload hinh
        clsTableAction clsTable = new clsTableAction("Page");
        string sql ="select max(id_p) from page";
        string id = (clsTable.runReturnSQL(sql) + 1).ToString();
        if (uploadImage(FileUploadImage, id + ".jpg"))
        {
            //Tien hanh luu
            sql = string.Format("insert into page values('{0}','{1}',N'{2}',N'{3}',N'{4}','{5}','{6}','{7}','{8}');", id.ToString(), cmbMenu.SelectedValue, txtTitle.Text, EditorSumary.Text.Replace("'", "`"), EditorContent.Text.Replace("'", "`"), DateTime.Now, "ltngoc", (!chkOK.Checked).ToString(), id + ".jpg");
            clsTable.runSQL(sql);
        }
        clsTable.free();
    }
    private Boolean uploadImage(FileUpload FileImage, string fileName)
    {
        try
        {
            if (FileImage.HasFile)
            {
                string path = Server.MapPath("../") + "/Page/" + fileName;
                FileImage.SaveAs(path);
                clsImages clsImg = new clsImages();
                clsImg.saveAndResizeImage(path, path, 115, 79, ".jpg");
                return true;
            }
            return false;
        }
        catch
        {
            return false;
        }
    }

}
